package com.ruoyi.system.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.SysRoleDeptVo;
import com.ruoyi.system.domain.bo.SysRoleDeptBo;
import com.ruoyi.system.service.ISysRoleDeptService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 角色和部门关联
 *
 * @author ruoyi
 * @date 2023-11-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/roleDept")
public class SysRoleDeptController extends BaseController {

    private final ISysRoleDeptService iSysRoleDeptService;

    /**
     * 查询角色和部门关联列表
     */
    @SaCheckPermission("system:roleDept:list")
    @GetMapping("/list")
    public TableDataInfo<SysRoleDeptVo> list(SysRoleDeptBo bo, PageQuery pageQuery) {
        return iSysRoleDeptService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出角色和部门关联列表
     */
    @SaCheckPermission("system:roleDept:export")
    @Log(title = "角色和部门关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysRoleDeptBo bo, HttpServletResponse response) {
        List<SysRoleDeptVo> list = iSysRoleDeptService.queryList(bo);
        ExcelUtil.exportExcel(list, "角色和部门关联", SysRoleDeptVo.class, response);
    }

    /**
     * 获取角色和部门关联详细信息
     *
     * @param roleId 主键
     */
    @SaCheckPermission("system:roleDept:query")
    @GetMapping("/{roleId}")
    public R<SysRoleDeptVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long roleId) {
        return R.ok(iSysRoleDeptService.queryById(roleId));
    }

    /**
     * 新增角色和部门关联
     */
    @SaCheckPermission("system:roleDept:add")
    @Log(title = "角色和部门关联", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysRoleDeptBo bo) {
        return toAjax(iSysRoleDeptService.insertByBo(bo));
    }

    /**
     * 修改角色和部门关联
     */
    @SaCheckPermission("system:roleDept:edit")
    @Log(title = "角色和部门关联", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysRoleDeptBo bo) {
        return toAjax(iSysRoleDeptService.updateByBo(bo));
    }

    /**
     * 删除角色和部门关联
     *
     * @param roleIds 主键串
     */
    @SaCheckPermission("system:roleDept:remove")
    @Log(title = "角色和部门关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roleIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] roleIds) {
        return toAjax(iSysRoleDeptService.deleteWithValidByIds(Arrays.asList(roleIds), true));
    }
}
